package fr.upstlse.glre.client.espaceperso.profil;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.espaceperso.EspaceEtudiant;
import fr.upstlse.glre.client.espaceperso.component.CenterPanel;
import fr.upstlse.glre.client.espaceperso.profil.action.ProfilEditionSubmit;
import fr.upstlse.glre.client.vo.user.EtudiantVOGWT;
import fr.upstlse.glre.client.widgets.fields.EtudiantFields;


/**
 * Construit le panneau de profil d'un étudiant. Ce panneau affiche
 * textuellement le profil d'un étudiant. Il offre la possibilité de le modifier
 * par l'intermédiaire d'un formulaire
 * 
 * @author michael
 * 
 */
public class EtudiantProfil extends Composite {

	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	ContentPanel profilPanel;


	/**
	 * Panneau HTML contenant le profil au format texte.
	 */
	Html profilReadingPanel;

	/**
	 * Panneau FORM contenant le profil au format formulaire pour l'édition.
	 */
	FormPanel profilEditingPanel;

	EtudiantVOGWT etudiant;

	/**
	 * Variable d'état pour le panneau de contenu. Vrai si le panneau courant
	 * est l'édition du profil, faux sinon
	 */
	boolean editionMode = false;

	/**
	 * Construit le panneau de profil. L'initialise avec le contenu en mode
	 * lecture (affichage du profil) Initialise le service.
	 * 
	 * @param pEtudiant
	 *            Etudiant dont on souhaite obtenir le profil
	 */
	public EtudiantProfil() {
		etudiant = EspaceEtudiant.getInstance().getUser();
		createProfilPanel();
		readingMode();
		editionMode();
		
		profilPanel.add(profilReadingPanel);
		editionMode = false;
		initWidget(profilPanel);
	}

	private void createProfilPanel() {
		profilPanel = new ContentPanel();
		profilPanel.setAutoWidth(true);
		profilPanel.setWidth(500);
		profilPanel.setId(RfgConstante.PROFIL_PANEL_ID);
		profilPanel.setBorders(true);

		// Draggable panel
		Draggable lDraggable = new Draggable(profilPanel, profilPanel
				.getHeader());
		lDraggable.setContainer(CenterPanel.getInstance());

		// Tool Button
		ToolButton lSwithToEditionMode = new ToolButton(".my-tool-gear");
		lSwithToEditionMode.setStyleAttribute("background",
				" transparent url(images/edit.png) no-repeat");
		lSwithToEditionMode
				.setToolTip(RfgConstante.PROFIL_PANEL_BUTTON_SWITCH_TOOLTIP);
		lSwithToEditionMode.addSelectionListener(new SelectionListener<ComponentEvent>() {
			public void componentSelected(ComponentEvent ce) {
				if (!editionMode) {
					
					profilPanel.add(profilEditingPanel);
					profilPanel.layout();
					profilPanel.remove(profilReadingPanel);
					profilPanel.layout();
					editionMode = true;
				} else {
					
					profilPanel.add(profilReadingPanel);
					profilPanel.layout();
					profilPanel.remove(profilEditingPanel);
					profilPanel.layout();
					editionMode = false;
				}
			}
		});
		profilPanel.setHeading(RfgConstante.PROFIL_PANEL_HEADER_TEXT);
		profilPanel.getHeader().addTool(lSwithToEditionMode);
	}

	private void editionMode() {
		// formulaire sans action
		profilEditingPanel = new FormPanel();
		profilEditingPanel.setHeaderVisible(false);
		profilEditingPanel.setAutoWidth(true);

		EtudiantFields lEtudiantFields = new EtudiantFields(etudiant, profilEditingPanel, true);
		
		Button lSubmit = new Button(
				RfgConstante.PROFIL_EDITING_FIELD_SUBMIT_LABEL);
		lSubmit.addSelectionListener(new ProfilEditionSubmit(lEtudiantFields, this));
		profilEditingPanel.addButton(lSubmit);

	}

	public void readingMode() {
		if (etudiant == null) {
			MessageBox lFailureMessageBox = MessageBox.alert(
					"Erreur accès étudiant",
					"L'étudiant n'est pas bien authentifié", null);
			lFailureMessageBox.show();
		} else {
			profilReadingPanel = etudiant.getHtml();
		}
	}

	public ContentPanel getProfilPanel() {
		return profilPanel;
	}

	public void setProfilPanel(ContentPanel profilPanel) {
		this.profilPanel = profilPanel;
	}

	public Html getProfilReadingPanel() {
		return profilReadingPanel;
	}

	public void setProfilReadingPanel(Html profilReadingPanel) {
		this.profilReadingPanel = profilReadingPanel;
	}

	public FormPanel getProfilEditingPanel() {
		return profilEditingPanel;
	}

	public void setProfilEditingPanel(FormPanel profilEditingPanel) {
		this.profilEditingPanel = profilEditingPanel;
	}

	public EtudiantVOGWT getEtudiant() {
		return etudiant;
	}

	public void setEtudiant(EtudiantVOGWT etudiant) {
		this.etudiant = etudiant;
	}

	public boolean isEditionMode() {
		return editionMode;
	}

	public void setEditionMode(boolean editionMode) {
		this.editionMode = editionMode;
	}
}
